package com.liusha.quicksand.web.admin;

import com.liusha.quicksand.po.Admin;
import com.liusha.quicksand.po.User;
import com.liusha.quicksand.service.admin.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 一世长安一世欢
 * @Date: 2020/10/19
 * @Time: 16:40
 * @Description: No Description
 */

@Controller
@RequestMapping("/admin")
public class AdminLoginController {

    @Autowired
    private AdminService adminService;

    @GetMapping
    public String loginPage() {

        return "admin/adminLogin";
    }

    @GetMapping("/adminIndex")
    public String adminIndex(){

        return "admin/adminIndex";
    }

    @PostMapping("/loginAdmin")
    public String login(@RequestParam String username,
                        @RequestParam String password,
                        HttpSession session,
                        RedirectAttributes attributes) {
        Admin admin = adminService.checkAdmin(username, password);
        if (admin != null) {
            admin.setPassword(null);
            session.setAttribute("admin", admin);
            return "admin/adminIndex";
        } else {
            attributes.addFlashAttribute("message", "用户名和密码错误");
            return "redirect:/admin";
        }
    }

//    退出登录状态
    @GetMapping("/adminLogout")
    public String adminLogout(HttpSession session) {
        session.removeAttribute("admin");
        return "redirect:/admin";
    }
}
